package com.cskaoyan.service.impl;

import com.cskaoyan.bean.pojo.MallRegion;
import com.cskaoyan.bean.pojo.MallRegionExample;
import com.cskaoyan.bean.vo.RegionListVo;
import com.cskaoyan.bean.vo.RegionListVo1;
import com.cskaoyan.bean.vo.RegionListVo2;
import com.cskaoyan.bean.vo.WxRegionListVo;
import com.cskaoyan.mapper.MallRegionMapper;
import com.cskaoyan.service.RegionListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: chaohe@com.cskaoyan
 * @Date: 2021/7/8
 * @Time: 21:08
 */
@Service
public class RegionListServiceImpl implements RegionListService {

    @Autowired
    MallRegionMapper mallRegionMapper;

    /**
     * @Author chaohe
     * @description: 商场管理的行政模块
     * 要求出省，市，县需要分层次查询
     * @Date 0:49 2021/7/9
     * url: http://localhost:8083/admin/region/list
     */
    @Override
    public List<RegionListVo> list() {

        //数据库获得的所有信息
        MallRegionExample mallRegionExample = new MallRegionExample();
        MallRegionExample.Criteria criteria = mallRegionExample.createCriteria();

        //查询pid为1的所有省
        criteria.andPidEqualTo(0);

        List<MallRegion> mallRegionList = mallRegionMapper.selectByExample(mallRegionExample);

        List<RegionListVo> regionListVoList = new ArrayList<>();

        for (MallRegion mallRegion : mallRegionList) {
            MallRegionExample mallRegionExample1 = new MallRegionExample();
            MallRegionExample.Criteria criteria1 = mallRegionExample1.createCriteria();

            //查询type=2，以及code范围内所有市，区
            criteria1.andTypeEqualTo(2);
            criteria1.andCodeBetween(mallRegion.getCode() * 100, mallRegion.getCode() * 100 + 99);
            List<MallRegion> mallRegionList1 = mallRegionMapper.selectByExample(mallRegionExample1);

            List<RegionListVo1> regionListVo1List = new ArrayList<>();

            for (MallRegion region : mallRegionList1) {
                MallRegionExample mallRegionExample2 = new MallRegionExample();
                MallRegionExample.Criteria criteria2 = mallRegionExample2.createCriteria();


                //查询type=3，code范围内的所有县
                criteria2.andTypeEqualTo(3);
                criteria2.andCodeBetween(region.getCode() * 100, region.getCode() * 100 + 99);

                List<MallRegion> mallRegionList2 = mallRegionMapper.selectByExample(mallRegionExample2);
                List<RegionListVo2> regionListVo2List = new ArrayList<>();
                for (MallRegion mallRegion1 : mallRegionList2) {
                    RegionListVo2 regionListVo2 = new RegionListVo2(mallRegion1.getId(), mallRegion1.getName(), mallRegion1.getType(), mallRegion1.getCode());
                    regionListVo2List.add(regionListVo2);
                }
                RegionListVo1 regionListVo1 = new RegionListVo1(region.getId(), region.getName(), region.getType(), region.getCode(), regionListVo2List);
                regionListVo1List.add(regionListVo1);

            }
            RegionListVo regionListVo = new RegionListVo(mallRegion.getId(), mallRegion.getName(), mallRegion.getType(), mallRegion.getCode(), regionListVo1List);
            regionListVoList.add(regionListVo);
        }



        return regionListVoList;
    }


    /**
     * @Author: zoey-zhu
     * @description: 小程序region获取地址：省、市、区
     * @Date: 15:31 2021/7/12
     * @url: http://182.92.235.201:8083/wx/region/list?pid=0
     */
    @Override
    public List<WxRegionListVo> listByPid(Integer pid) {

        //数据库获得的所有信息
        MallRegionExample mallRegionExample = new MallRegionExample();
        MallRegionExample.Criteria criteria = mallRegionExample.createCriteria();

        //查询pid为1的所有省
        criteria.andPidEqualTo(pid);

        List<MallRegion> mallRegionList = mallRegionMapper.selectByExample(mallRegionExample);
        List<WxRegionListVo> vxRegionVoList = new ArrayList<>();

        for (MallRegion mallRegion : mallRegionList) {
            WxRegionListVo vxRegion = new WxRegionListVo(mallRegion.getId(), mallRegion.getPid(), mallRegion.getName(), mallRegion.getType(), mallRegion.getCode());
            vxRegionVoList.add(vxRegion);
        }

        return vxRegionVoList;
    }
}
